package org.ezplatform.office.forum.web.rest;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.ezplatform.cloud.api.component.popuser.PopUserScopeUtilService;
import org.ezplatform.cloud.api.system.user.UserApiService;
import org.ezplatform.cloud.common.client.RestClient;
import org.ezplatform.cloud.common.log.LogService;
import org.ezplatform.component.permission.service.CmpApplyScopeService;
import org.ezplatform.core.annotation.MenuData;
import org.ezplatform.core.common.WebUser;
import org.ezplatform.core.exception.ValidationException;
import org.ezplatform.core.pagination.PropertyFilter;
import org.ezplatform.core.security.SecurityUtils;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.core.web.controller.BaseController;
import org.ezplatform.core.web.util.WebUtils;
import org.ezplatform.core.web.view.OperationResult;
import org.ezplatform.office.forum.entity.ForumBoard;
import org.ezplatform.office.forum.entity.ForumPost;
import org.ezplatform.office.forum.service.ForumBoardService;
import org.ezplatform.office.forum.service.ForumPostLabelService;
import org.ezplatform.office.forum.service.ForumPostService;
import org.ezplatform.office.forum.service.ForumSensitiveService;
import org.ezplatform.office.forum.service.ForumTypeService;
import org.ezplatform.office.forum.util.forumpostsend.ForumPostSendUtils;
import org.ezplatform.util.GlobalConstant;
import org.ezplatform.util.JsonUtils;
import org.ezplatform.util.StringUtils;
import org.ezplatform.util.ValidateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;

/**
 * 我的论坛控制类
 * 2019年11月2日17:40:59
 * @author Administrator
 *
 */
@RestController
@SuppressWarnings("rawtypes")
@RequestMapping(value = "/api/w/forum/post")
public class ForumPostWebApiController extends BaseController<ForumPost, String>{
	
private final Logger LOGGER = LoggerFactory.getLogger(ForumPostWebApiController.class);
	
	@Autowired
	private UserApiService userApiService;
	
	@Autowired
	private ForumTypeService forumTypeService;
	
	@Autowired
	private CmpApplyScopeService cmpApplyScopeService;
	
	@Autowired
	private ForumPostService forumPostService;
	
	@Autowired
	private ForumPostLabelService forumPostLabelService;

	@Autowired
	private ForumBoardService forumBoardService;
	
	@Autowired
	private ForumSensitiveService forumSensitiveService;
	
	@Autowired
	private PopUserScopeUtilService popUserScopeUtilService;
	
	@Autowired
	private LogService logService;
	
	@Autowired
    private RestClient restClient;

	@Override
	protected BaseService<ForumPost, String> getEntityService() {
		return forumPostService;
	}
	
	/**
	 * 初始化数据
	 *
	 * @param request
	 * @param model
	 * @param id
	 */
	@ModelAttribute
	public void prepareModel(HttpServletRequest request, Model model,
			@RequestParam(value = "id", required = false) String id) {
		super.initPrepareModel(request, model, id);
	}
	
	/**
	 * 所有帖子列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/allpostList", method = RequestMethod.GET)
	@ResponseBody
	public Page<Map> findAllPostByPage(HttpServletRequest request) {
		String corpId = WebUtils.getCurrentUser().getCorpId();
		String boardId = request.getParameter("boardId");
		if("1".equals(boardId)) {
			boardId = null;
		}
		String pId = request.getParameter("pId");
		String searchContent = request.getParameter("searchContent");
		//searchType 0:代表所有,1:代表作者.2:代表标题
		String searchType = request.getParameter("searchType");
		//forumType 版块类别(最新,1;最热,2;精华,3;公告,4)
		String forumType = request.getParameter("forumType");
		String treeType=request.getParameter("treeType");//所有帖子不受影响，导航来的话受影响   父级 为1   子级为2
		Pageable pageable = PropertyFilter.buildPageableFromHttpRequest(request);
		List<Map> map1 = new ArrayList<Map>();
		List<Map> map2 = new ArrayList<Map>();
		List<PropertyFilter> filter = PropertyFilter.buildFiltersFromHttpRequest(ForumPost.class, request);
		Page<Map> page = null;
		page= forumPostService.allPostList(pageable, filter, TOKEN, corpId, boardId, "",pId,"",treeType,"",forumType,searchContent,searchType);
		List<Map> list = page.getContent();
		if((pId!=null && !"null".equals(pId)&&StringUtils.isNotBlank(pId))||StringUtils.equals("-1", pId)) {
			for (Map map : list) {
				boolean flag = false;
				List<Map<String, Object>> scopeName = (List<Map<String, Object>>) map.get("labels");
				for (Map<String, Object> mapValue : scopeName) {
					if("公告".equals(mapValue.get("name").toString()) && "true".equals(mapValue.get("checked").toString())) {
						map.put("flag", true);
						map1.add(map);
						flag = true;
					}
				}
				if(!flag) {
					map.put("flag", false);
					map2.add(map);
				}
			}
			map1.addAll(map2);
			list = map1;
		}
		return new PageImpl<>(list, page.getPageable(), page.getTotalElements());
	}
	
	/**
	 * 所有帖子
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/searchAllPost", method = RequestMethod.GET)
	@ResponseBody
	public Page<Map> searchAllPost(HttpServletRequest request) {
		WebUser user = WebUtils.getCurrentUser();
		Pageable pageable = PropertyFilter.buildPageableFromHttpRequest(request);
		List<PropertyFilter> filter = PropertyFilter.buildFiltersFromHttpRequest(ForumPost.class, request);
		String title = ValidateUtils.filter(request.getParameter("title"));
		String publishType = request.getParameter("publishType");
		String typeId = request.getParameter("typeId");
		String boardId = request.getParameter("boardId");
		String author = request.getParameter("author");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String searchType = request.getParameter("searchType");
		String mdpostType = StringUtils.null2String(request.getParameter("mdpostType"));
		//1.最新发帖 2.最热 3.精华 4.最新评论
		String tab = request.getParameter("tab");
		if(StringUtils.isEmpty(tab)) {
			tab = "1";
		}
		Page<Map> page = forumPostService.searchAllPost(title,publishType,typeId,boardId,author,startTime,endTime,user,pageable,tab,searchType,filter,mdpostType);
		return page;
	}
	
	/**
	 * 查看帖子
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/viewPost", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult viewPost(@RequestParam("id") String id, HttpServletRequest request) {
		ForumPost f  = forumPostService.findOne(id);
		WebUser user = WebUtils.getCurrentUser();
		if(f == null) {
			return null;
		}
		if(StringUtils.equals(f.getCheckStatus(), ForumPost.CheckStatusEnum.CHECK_STATUS_SHBTG.getValue())) {
			ForumPostSendUtils.takeMsgDone(f, user.getCorpId(), user.getJwtToken(), user.getUserId());
		}
		String checkStatus = StringUtils.null2String(request.getParameter("checkStatus"));
		if(ValidateUtils.validateId(checkStatus) == false){
        	return OperationResult.buildFailureResult("非法参数!");
		}
		int status = StringUtils.toInteger(checkStatus);
		String currentPage = request.getParameter("page");
		//防止次数加1
		String onlyId = request.getParameter("onlyId");
		Pageable pageable = PropertyFilter.buildPageableFromHttpRequest(request);
		List<PropertyFilter> filter = PropertyFilter.buildFiltersFromHttpRequest(ForumPost.class, request);
		String basicUrl = GlobalConstant.getFlexServerPath(null);
		Map map= forumPostService.viewPost(pageable, filter, f, WebUtils.getCurrentUser().getJwtToken(), currentPage, status, "",onlyId,basicUrl);
		//Map map= forumPostService.viewPost(pageable, filter, id, WebUtils.getCurrentUser().getJwtToken(), currentPage, status, "",onlyId,basicUrl);
		if(StringUtils.isNotBlank(request.getParameter("logCode")) && f.getType() == 1) {
			if(StringUtils.equals("1", onlyId)) {
				//forumSensitiveService.saveviewPostlog(request.getParameter("logCode")+":03", f.getTitle());
				forumTypeService.saveModuleLog(request.getParameter("logCode")+":03", f.getTitle(), ""); 
				//forumPostService.saveModuleLog(request.getParameter("logCode")+":03", f.getTitle(),"");
			}			
		}
		return OperationResult.buildSuccessResult(map);
	}
	
	/**
	 * 查询帖子当前状态
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/realCheckStatus", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult realCheckStatus(@RequestParam("id") String id, HttpServletRequest request) {
		ForumPost f  = forumPostService.findOne(id);
		if(f == null) {
			return null;
		}
		Map map = new HashMap();
		map.put("checkStatus", f.getCheckStatus().toString());
		return OperationResult.buildSuccessResult(map);
	}
	
	/**
	 * 根据id获取信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/info", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult editShow(@RequestParam("id") String id, HttpServletRequest request) {
		int type = StringUtils.toInteger(request.getParameter("checkStatus"));
		Map m =  forumPostService.findOnePost(id, type, getToken(), "",WebUtils.getCurrentUser().getCorpId(),WebUtils.getCurrentUser());
		WebUser user = WebUtils.getCurrentUser();
		m.put("isAuthor", m.get("author").toString().equals(user.getUserId()));
		//获取系统设置的附件限制信息
		Map<String, String> textParams = Maps.newHashMap();
		textParams.put("mark", "sysAttachmentSize");
		textParams.put("corpId", user.getCorpId());
		String roleStr1 = this.restClient.get("/system/config/getConfigValue", user.getJwtToken(), textParams);
		Map<String, Object> result1 = JsonUtils.readValue(roleStr1);
		
		if(result1.get("data")!=null) {
			m.put("fileSingleSizeLimit", result1.get("data").toString());
		}else {
			m.put("fileSingleSizeLimit", "");
		}
		return OperationResult.buildSuccessResult(m);
	}
	
	/**
	 * 删除（批量）数据
	 */
	@RequestMapping(value = "/deleteMyPost", method = RequestMethod.POST)
	@ResponseBody
	public OperationResult deleteMyPost(@RequestParam("ids") String... ids) {
		OperationResult result;
		Map r = forumPostService.checkMyDelete(ids);
		if ((boolean) r.get("flag"))
			result = forumPostService.deleteMyPost(ids, r);
		else
			result = new OperationResult(OperationResult.OPERATION_RESULT_TYPE.failure, "帖子所属的版块需要审核无法删除帖子");
		return result;
	}
	
	/**
	 * 我的帖子列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/mypostList", method = RequestMethod.GET)
	@ResponseBody
	public Page<Map> myPostList(HttpServletRequest request) {
		String corpId = WebUtils.getCurrentUser().getCorpId();
		String type = request.getParameter("type");
		String checkStatus = request.getParameter("checkStatus");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		Pageable pageable = PropertyFilter.buildPageableFromHttpRequest(request);
		List<PropertyFilter> filter = PropertyFilter.buildFiltersFromHttpRequest(ForumPost.class, request);
		Page<Map> page = forumPostService.myPostList(pageable, filter, TOKEN, corpId, type,"","",checkStatus,startTime,endTime);
		return page;
	}
	
	/**
	 * 我的草稿列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/mydraftList", method = RequestMethod.GET)
	@ResponseBody
	public Page<Map> mydraftList(HttpServletRequest request) {
		String corpId = WebUtils.getCurrentUser().getCorpId();
		String type = request.getParameter("type");
		String checkStatus = request.getParameter("checkStatus");
		Pageable pageable = PropertyFilter.buildPageableFromHttpRequest(request);
		List<PropertyFilter> filter = PropertyFilter.buildFiltersFromHttpRequest(ForumPost.class, request);
		Page<Map> page = forumPostService.mydraftList(pageable, filter, TOKEN, corpId, type,"","",checkStatus);
		return page;
	}
	
	/**
	 * 我的收藏列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/favoriteList", method = RequestMethod.GET)
	@ResponseBody
	public Page<Map> favoriteList(HttpServletRequest request) {
		String corpId = WebUtils.getCurrentUser().getCorpId();
		String type = request.getParameter("type");
		Pageable pageable = PropertyFilter.buildPageableFromHttpRequest(request);
		List<PropertyFilter> filter = PropertyFilter.buildFiltersFromHttpRequest(ForumPost.class, request);
		Page<Map> page = forumPostService.favoriteList(pageable, filter, TOKEN, corpId, type,"","");
		return page;
	}
	
	/**
	 * 获取所有可发帖版块(黑名单模式,即显示所有没有被删掉的模块让用户选,点击确定以后再判断是否在黑名单内)
	 * @param request
	 * @return
	 */
	@MenuData("可发帖版块")
	@RequestMapping(value = "/postBoards", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult postBoards(HttpServletRequest request) {
    	//获取版块id
        String boardId = request.getParameter("boardId");
        String logCode = request.getParameter("logCode");
        WebUser user = WebUtils.getCurrentUser();
		Map map =  forumBoardService.postBoards(boardId,user.getUserId());
		return OperationResult.buildSuccessResult(map);
	}
	
	/**
	 * 获取所有可发帖版块(我的帖子显示的版块 不管类别的禁用状态)
	 * @param request
	 * @return
	 */
	@MenuData("我的帖子显示的版块 不管类别的禁用状态")
	@RequestMapping(value = "/postBoardsIngoreStatus", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult postBoardsIngoreStatus(HttpServletRequest request) {
		WebUser user = WebUtils.getCurrentUser();
		List<Map> list = forumBoardService.postBoardsIngoreStatus(user);
		return OperationResult.buildSuccessResult(list);
	}
	
	/**
	 * 置顶帖子
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/topPost", method = RequestMethod.POST)
	@ResponseBody
	public OperationResult topPost(@RequestParam("id") String id) {
		//SQL盲注漏洞处理
        if(!ValidateUtils.validateId(id)) {
            throw new ValidationException("pageTitle非法参数");
        }
		return forumPostService.topPost(id);
	}
	
	/**
	 * 给帖子设置精华
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/essencePost", method = RequestMethod.POST)
	@ResponseBody
	public OperationResult essencePost(@RequestParam("id") String id) {
		return forumPostService.essencePost(id);
	}
	
	/**
	 * 给帖子添加标签
	 * @param request
	 * @param labelId
	 * @param postIds
	 * @return
	 */
	@RequestMapping(value = "/labelPost", method = RequestMethod.POST)
	@ResponseBody
	public OperationResult labelPost(HttpServletRequest request, @RequestParam("labelId") String labelId,
			@RequestParam("postIds") String... postIds) {
		String isFlush = request.getParameter("isFlush");
		return forumPostLabelService.labelPost(labelId, isFlush, postIds);
	}
	
	/**
	 * 删除帖子,回帖
	 * @param request
	 * @param ids
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public OperationResult deletePost(HttpServletRequest request,@RequestParam("ids") String... ids) throws ParseException {
		OperationResult result;
		String logCode = StringUtils.null2String(request.getParameter("logCode"));
		if(!StringUtils.isNoneBlank(logCode)) {
			if(!logCode.contains("draft")) {
				if(StringUtils.null2String(request.getParameter("pagetype")).equals("board")) {
		        	logCode = "forum:navigation:04";
		        }
		        if(StringUtils.null2String(request.getParameter("pagetype")).equals("mypost")) {
		        	logCode = "forum:mypost:04";
		        }
			}
		}
		result = forumPostService.deletePost(ids,logCode);
		return result;
	}
	
	/**
	 * 收藏/取消收藏帖子
	 * @param request
     * @param id
     * @return
	 */
    @RequestMapping(value = "/favoritePost", method = RequestMethod.POST)
    @ResponseBody
	public OperationResult favoritePost(HttpServletRequest request, @RequestParam("ids") String id) {
		OperationResult result;
        WebUser user = WebUtils.getCurrentUser();
        result = forumPostService.favoritePost(id, user.getUserId(), user.getCnName(),user.getCorpId(),user);
		return result;
	}
    
    /**
     * 编辑、保存帖子信息
     * @param entity
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/editPost", method = RequestMethod.POST)
    @ResponseBody
    public OperationResult editSave(@ModelAttribute("entity") ForumPost entity, HttpServletRequest request, Model model){
        if(!ValidateUtils.validateChar(entity.getTitle(), "%+")){
            return OperationResult.buildFailureResult("+、%是非法参数，禁止输入");
		}
        if(ValidateUtils.validateId(entity.getAuthor()) == false){
        	return OperationResult.buildFailureResult("非法参数!");
		}
        //作者
        if(StringUtils.isNotBlank(request.getParameter("author"))) {
            entity.setAuthor((request.getParameter("author")));
        }
        if(!ValidateUtils.validateChar(entity.getAttId(), "%+")){
        	return OperationResult.buildFailureResult("非法参数!");
  		}
        if(!ValidateUtils.validateChar(entity.getCheckStatus(), "%+")){
        	return OperationResult.buildFailureResult("非法参数!");
 		}
        String content1 = request.getParameter("content1");
        String content2 = request.getParameter("content2");
        String switchType = request.getParameter("switchType");
        if(StringUtils.equals(switchType, "1")) {
        	entity.setContent(content1);
        }else {
        	entity.setContent(content2);
        }
        //编辑页面的入口(我的帖子,版块导航,我的草稿,我收藏的)
        String logCode = "";
        if(StringUtils.isNotBlank(request.getParameter("id"))) {
        	if (!request.getParameter("logCode").toString().contains("draft")) {
        		logCode = request.getParameter("logCode")+":02"; 
            }if (!StringUtils.equals(entity.getCheckStatus(), ForumPost.CheckStatusEnum.CHECK_STATUS_CG.getValue()) && request.getParameter("logCode").toString().contains("draft")) {
            	logCode = request.getParameter("logCode")+":01"; 
            }	
		}else {
			if (!StringUtils.equals(entity.getCheckStatus(), ForumPost.CheckStatusEnum.CHECK_STATUS_CG.getValue()) && StringUtils.isNotBlank(entity.getCheckStatus())) {
				logCode = request.getParameter("logCode")+":01"; 
			}
		}
        return forumPostService.editSave(entity, false,request,logCode);
    }
    
    /**
     * 发送草稿箱中的草稿
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/sendPost", method = RequestMethod.POST)
	@ResponseBody
	public OperationResult deletePost(@RequestParam("id") String id, HttpServletRequest request) {
		OperationResult result;
		WebUser user = WebUtils.getCurrentUser();
		result = forumPostService.sendPost(id,user,request);
		return result;
	}
    
    /**
     * 待审核列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/checkList", method = RequestMethod.GET)
    @ResponseBody
    public Page<Map> checkList(HttpServletRequest request){
    	String corpId = WebUtils.getCurrentUser().getCorpId();
 		Pageable pageable = PropertyFilter.buildPageableFromHttpRequest(request);
 		List<PropertyFilter> filter = PropertyFilter.buildFiltersFromHttpRequest(ForumPost.class, request);
    	Page<Map> page = forumPostService.checkList(pageable, filter, getToken(),"","",corpId);
    	return page;
    }
    
    /**
     * 审核帖子
     * @param request
     * @param ids
     * @return
     */
    @RequestMapping(value = "/checkPostInfo", method = RequestMethod.POST)
    @ResponseBody
    public OperationResult checkPostInfo(HttpServletRequest request,@RequestParam("ids") String... ids){
    	String type = request.getParameter("type");
    	if(StringUtils.isBlank(type)) {
    		type = "1";
    	}
    	for(String id : ids) {
    		ForumPost post = forumPostService.findOne(id);
    		logService.saveModuleLog("forum:check:01",post.getTitle(), "");
    	}
    	return forumPostService.checkPostInfo(ids,type,"",request);
    }
    
    /**
     * 驳回帖子
     * @param entity
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/returnPostInfo", method = RequestMethod.POST)
    @ResponseBody
    public OperationResult returnPostInfo(@ModelAttribute("entity") ForumPost entity, HttpServletRequest request, Model model){
    	String[] ids = {entity.getId()};
    	for(String id : ids) {
    		ForumPost post = forumPostService.findOne(id);
    		logService.saveModuleLog("forum:check:02",post.getTitle(), "");
    	}
    	return forumPostService.checkPostInfo(ids,ForumPost.CheckStatusEnum.CHECK_STATUS_SHBTG.getValue(),entity.getRejection(),request);
    }
    
    /**
     * 移动帖子
     * @param id
     * @param request
     * @param postId
     * @return
     */
    @RequestMapping(value = "/changePostStatus", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult changePostStatus(@RequestParam("id") String id,HttpServletRequest request,@RequestParam("postId") String... postId) {
		return forumPostService.changePostStatus(postId,id,request);
	}
    
    /**
     * 帖子标签的变动
     * @param ids
     * @param postId
     * @return
     */
    @RequestMapping(value = "/changeLabel", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult changeLabel(@RequestParam("ids") String ids,@RequestParam("postId") String postId) {
		return forumPostService.changeLabel(postId,ids);
	}
    
    /**
     * 回复帖子
     * @param entity
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/replyPost", method = RequestMethod.POST)
    @ResponseBody
    public OperationResult replyPost(@ModelAttribute("entity") ForumPost entity, HttpServletRequest request, Model model){
    	if(ValidateUtils.validateId(entity.getBoardId()) == false){
    		return OperationResult.buildFailureResult("非法参数!");
	  }
    	if(!ValidateUtils.validateChar(entity.getAttId(), "%+")){
    		return OperationResult.buildFailureResult("非法参数!");
		  }
	  if(!ValidateUtils.validateChar(entity.getContent(), "%+") ){
		  return OperationResult.buildFailureResult("+、%是非法参数，禁止输入");
		  }
	  if(!ValidateUtils.validateInt(String.valueOf(entity.getIsAnonymity())) ){
		  return OperationResult.buildFailureResult("非法参数!");
	  }
	    entity.setIsRead(1);
    	return forumPostService.replyPost(entity,request,null,null);
    }
    
    /**
     * 根据版块id获取版块信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/boardInfo", method = RequestMethod.GET)
    @ResponseBody
    public OperationResult editShow(@RequestParam("id") String id) {
        ForumBoard b = forumBoardService.findOne(id);
        Map m = null;
        try {
        	if(b != null) {
        		m = BeanUtils.describe(b);
	            //String userStr = popUserScopeUtilService.getScopeTypeValue("oa_forum_board_user_scope", "oa_forum_board", "id", StringUtils.null2String(b.getId()));
	            if(StringUtils.isBlank(b.getModerator())) {
	            	String moderatorStr = popUserScopeUtilService.getScopeTypeValue("oa_forum_moderator_scope", "oa_forum_board", "id", StringUtils.null2String(b.getId()));
	            	String moderatorNameStr = userApiService.getScopeName(moderatorStr, WebUtils.getCurrentUser().getCorpId(),WebUtils.getCurrentUser().getJwtToken());
		            m.put("moderator", moderatorNameStr);
	            }
	            
	            // m.put("userStr", userStr);
	            int sendPost = forumBoardService.getPostNum(b.getId(),"1",WebUtils.getCurrentUser().getCorpId());
	            m.put("sendPostNum", sendPost);
	            int rePost = forumBoardService.getPostNum(b.getId(),"2",WebUtils.getCurrentUser().getCorpId());
	            m.put("rePostNum", rePost);
        	}
        } catch (IllegalAccessException e) {
            LOGGER.error(e.getMessage());
        } catch (NoSuchMethodException e) {
            LOGGER.error(e.getMessage());
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return OperationResult.buildSuccessResult(m);
    }
    
    /**
     * 判断用户是否是系统管理员
     * @param request
     * @return
     */
    @RequestMapping(value = "/userIsAdmin", method = RequestMethod.GET)
    @ResponseBody
    public OperationResult userIsAdmin(HttpServletRequest request){
        Map m = new HashMap();
        WebUser user = WebUtils.getCurrentUser();
        // 判断当前用户是否是系统管理员或者应用管理员
        if(SecurityUtils.isSuperAdmin(user)||user.getRoleCodes().contains(ForumPost.CheckStatusEnum.YYGLYQX.getValue())){
            m.put("isAdmin", true);
        } else {
            m.put("isAdmin", false);
        }
        return OperationResult.buildSuccessResult(m) ;

    }
    
    /**
     * 帖子中某一个楼层的回复列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/allCommentList", method = RequestMethod.GET)
    @ResponseBody
    public Page<Map> allCommentList(HttpServletRequest request){
    	String corpId = WebUtils.getCurrentUser().getCorpId();
    	String id = request.getParameter("id");
 		Pageable pageable = PropertyFilter.buildPageableFromHttpRequest(request);
 		WebUser user = WebUtils.getCurrentUser();
 		List<PropertyFilter> filter = PropertyFilter.buildFiltersFromHttpRequest(ForumPost.class, request);
    	Page<Map> page = forumPostService.allCommentList(pageable, filter, getToken(),corpId,id,user);
    	return page;
    }
    
    /**
     * 在帖子某一楼层中回复
     * @param entity
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/submitComment", method = RequestMethod.POST)
    @ResponseBody
    public OperationResult submitComment(@ModelAttribute("entity") ForumPost entity, HttpServletRequest request, Model model){
		//3为评论回帖,4为回复评论
    	if(ValidateUtils.validateId(request.getParameter("level")) == false){
        	return OperationResult.buildFailureResult("非法参数!");
		}
    	if(ValidateUtils.validateId(request.getParameter("receiversId")) == false){
        	return OperationResult.buildFailureResult("非法参数!");
		}
		String level = request.getParameter("level");
		String receiversId = request.getParameter("receiversId");
    	if(ValidateUtils.validateId(entity.getBoardId()) == false){
    		return OperationResult.buildFailureResult("非法参数!");
	  }
	  if(!ValidateUtils.validateInt(String.valueOf(entity.getIsAnonymity())) ){
		  return OperationResult.buildFailureResult("非法参数!");
	  }
	  //默认评论为无需审核
	  entity.setCheckStatus("4");
	  //设置未读
	  entity.setIsRead(1);
      return forumPostService.replyPost(entity,request,level,receiversId);
    }
    
    /**
     * 用户是否可以在版块下发帖(指的是点击左侧版块树 选中某一个版块后再点击发帖)
     * @param request
     * @return
     */
    @RequestMapping(value = "/ifPost", method = RequestMethod.GET)
    @ResponseBody
    public OperationResult ifPost(HttpServletRequest request){
    	//判断是否管理员或者版主,可以发公告贴,普通用户只有主题帖权限
    	boolean ifAdminOrModerator = true;
    	//进行黑名单 过滤 不允许发帖
    	//获取当前用户
    	Map<String, Object> data = new HashMap();
        WebUser user = WebUtils.getCurrentUser();
        //获取公司id
        String corpId = user.getCorpId();
        String token = user.getJwtToken();
        if(!user.getRoleCodes().contains("appAdminRole")) {
        	//获取版块id
            String boardId = request.getParameter("boardId");
            //查找当前版主集合
            String moderatorScopeValue = popUserScopeUtilService.getScopeTypeValue("oa_forum_moderator_scope","oa_forum_board", "id",StringUtils.null2String(boardId));
            String moderatorScopeMap = userApiService.getScopeName(moderatorScopeValue,corpId,token);
            List<String> moderatorList = Arrays.asList(moderatorScopeMap.split(","));
            //如果当前用户不是版主,需要继续判断
            if(!moderatorList.contains(user.getCnName())) {
            	ifAdminOrModerator = false;
            	//查找当前版块黑名单集合
                String blackListScopeValue = popUserScopeUtilService.getScopeTypeValue("oa_forum_board_user_scope","oa_forum_board", "id",StringUtils.null2String(boardId));
                String blackListScopeMap = userApiService.getScopeName(blackListScopeValue,corpId,token);
                //如果黑名单是空的,默认所有人发帖
                if(blackListScopeMap==null || "".equals(blackListScopeMap)) {
                	data.put("ifAdminOrModerator", false);
                	data.put("ifPost", "true");
                	return OperationResult.buildSuccessResult(data);
                }
                List<String> allUserIds = cmpApplyScopeService.getAllOrgStaGrpIdsByUserId(user.getUserId());
                for(String id : allUserIds) {
                	if(blackListScopeValue.indexOf(id) != -1) {
                		data.put("ifAdminOrModerator", false);
                    	data.put("ifPost","false");
                    	return OperationResult.buildSuccessResult(data);
                	}
                }
            }
        } 
        data.put("ifAdminOrModerator", ifAdminOrModerator);
        data.put("ifPost", "true");
        return OperationResult.buildSuccessResult(data);
    }
    
    /**
     * 论坛动态获取我回复的,评论我的,@我的
     * @param request
     * @return
     */
    @RequestMapping(value = "/remindPost", method = RequestMethod.GET)
    @ResponseBody
    public OperationResult remindPost(HttpServletRequest request){
        WebUser user = WebUtils.getCurrentUser();
        String currentPage = request.getParameter("page");
        //1.我的回复2.评论我的3.@我的
        String remindPageType = request.getParameter("remindPageType");
        Pageable pageable = PropertyFilter.buildPageableFromHttpRequest(request);
        List<PropertyFilter> filter = PropertyFilter.buildFiltersFromHttpRequest(ForumPost.class, request);
        Map map= forumPostService.getRemindPostAll(pageable, filter, currentPage, user,remindPageType);
        return OperationResult.buildSuccessResult(map) ;
    }
    
    /**
     * 论坛动态逻辑删除
     * @param request
     * @param id
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/deleteRemind", method = RequestMethod.POST)
	@ResponseBody
	public OperationResult deleteRemind(HttpServletRequest request,@RequestParam("id") String id) throws ParseException {
		OperationResult result;
		String[] ids = {id};
		return forumPostService.deletePost(ids,"forum:navigation:04");
	}
    
    /**
	 * 根据传过来的id查询对应帖子所在的主贴 的 页数 和楼层数
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/pageCount", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult pageCount(HttpServletRequest request){
		//获取传入评论或回帖的postId值
		String id = request.getParameter("id");
		//获取对应主题帖的 总记录数 获取 当前传入评论/回帖 所在楼层
		Map<String , String> data = forumPostService.pageCount(id);
		return OperationResult.buildSuccessResult(data);
	}
	
    /**
     * 查询所有帖子页面的类型和版块
     * @param request
     * @return
     */
    @RequestMapping(value = "/searchList", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult searchList(HttpServletRequest request){
    	WebUser user = WebUtils.getCurrentUser();
    	String id = StringUtils.null2String(request.getParameter("id"));
    	String mdpostType = StringUtils.null2String(request.getParameter("mdpostType"));
        List<Map> boardMap =  forumBoardService.getBoardMap(user,id,mdpostType);
        List<Map> typeMap = forumTypeService.getTypeMap();
        Map map = new HashMap();
        map.put("board", boardMap);
        map.put("type", typeMap);
        return OperationResult.buildSuccessResult(map);
	}
    
}